package com.engine4cloud.tdf.supervision.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.engine4cloud.tdf.common.core.exception.ValidateCodeException;
import com.engine4cloud.tdf.common.data.web.query.Pages;
import com.engine4cloud.tdf.common.data.web.response.PageInfo;
import com.engine4cloud.tdf.common.data.web.response.Params;
import com.engine4cloud.tdf.supervision.convert.JzdxGroupMembersConvert;
import com.engine4cloud.tdf.supervision.dto.create.JzdxGroupMembersCreateDTO;
import com.engine4cloud.tdf.supervision.dto.query.JzdxGroupMembersQueryDTO;
import com.engine4cloud.tdf.supervision.dto.result.JzdxGroupMembersResultDTO;
import com.engine4cloud.tdf.supervision.dto.result.JzdxResultDTO;
import com.engine4cloud.tdf.supervision.dto.update.JzdxGroupMembersUpdateDTO;
import com.engine4cloud.tdf.supervision.entity.JzdxGroupMembersEntity;
import com.engine4cloud.tdf.supervision.mapper.JzdxGroupMembersMapper;
import com.engine4cloud.tdf.supervision.service.JzdxGroupMembersService;
import com.engine4cloud.tdf.supervision.service.JzdxService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.Optional;

/**
 * 矫正小组成员信息采集表
 *
 * @author wenjie
 * @date 2024-04-16 14:52:31
 */
@Service
public class JzdxGroupMembersServiceImpl extends ServiceImpl<JzdxGroupMembersMapper, JzdxGroupMembersEntity> implements JzdxGroupMembersService {
    @Resource
    private JzdxService jzdxService;

    @Override
    public PageInfo<JzdxGroupMembersResultDTO> getPageList(Params pageable, JzdxGroupMembersQueryDTO dto) {
        IPage<JzdxGroupMembersResultDTO> page = this.baseMapper.pageList(Pages.page(pageable), dto);
        return Pages.convert(page);
    }

    @Override
    public JzdxGroupMembersResultDTO getById(Long id) {
        return this.baseMapper.findById(id);
    }

    @Override
    public Boolean create(JzdxGroupMembersCreateDTO dto) {
        JzdxResultDTO dx = Optional.ofNullable(jzdxService.getByCode(dto.getRybh()))
                .orElseThrow(() -> new ValidateCodeException("矫正对象数据不存在"));

        return super.save(JzdxGroupMembersConvert.INSTANCE.create(dto, dx.getTenantId()));
    }

    @Override
    public Boolean update(JzdxGroupMembersUpdateDTO dto) {
        JzdxGroupMembersEntity old = Optional.ofNullable(this.baseMapper.selectById(dto.getId()))
                .orElseThrow(() -> new ValidateCodeException("数据不存在"));

        return updateById(JzdxGroupMembersConvert.INSTANCE.update(dto));
    }

}